package logic

import (
	"2210a/practice/models/mysql"
	"2210a/practice/models/redisinit"
	"2210a/practice/pkg"
	"context"
	"fmt"

	"2210a/practice/practicerpc/internal/svc"
	"2210a/practice/practicerpc/practicerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginUserLogic) LoginUser(in *practicerpc.LoginUserRequest) (*practicerpc.LoginUserResponse, error) {
	// todo: add your logic here and delete this line
	var login mysql.User
	mysql.Db.Debug().Where("mobile = ?", in.Mobile).Find(&login)
	if login.ID == 0 {
		return nil, fmt.Errorf("当前用户没注册，请重新注册")
	}

	get := redisinit.ClientRedis.Get("codelogin" + in.Mobile)
	if get.Val() != in.Code {
		return nil, fmt.Errorf("获取短信发送信息错误，请重新输入 ")
	}

	if login.Password != pkg.Md5Encryption(in.Password) {
		return nil, fmt.Errorf("密码不一致，请重新输入")
	}
	//redisinit.ClientRedis.Del("codelogin" + in.Mobile)
	return &practicerpc.LoginUserResponse{Id: int64(login.ID)}, nil
}
